package com.situ.day14;

import static org.hamcrest.CoreMatchers.nullValue;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.channels.FileLockInterruptionException;
import java.util.Arrays;

import org.junit.Test;

public class IODemo {
	
	@Test
	public void fileReaderTest() {
		try {
			FileReader fileReader = new FileReader("io.txt");
			//public int read() 读取单个字符,如果已到达流的末尾，则返回 -1 
			int ch1 = fileReader.read();
			System.out.println((char)ch1);//a
			int ch2 = fileReader.read();
			System.out.println((char)ch2);//b
			int ch3 = fileReader.read();
			System.out.println((char)ch3);//c
			int ch4 = fileReader.read();
			System.out.println(ch4);//-1
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void fileReaderTest2() {
		try {
			//FileReader fileReader = new FileReader("io.txt");
			FileReader fileReader = new FileReader("E://io3.txt");
			//FileReader fileReader = new FileReader("src/com/situ/day14/io1.txt");
			//public int read() 读取单个字符,如果已到达流的末尾，则返回 -1 
			int ch = -1;
			//while循环去读，直到ch的值等于-1，就退出循环
			while ((ch = fileReader.read()) != -1) {
				System.out.println((char)ch);
			}
			
			ch = fileReader.read();
			while (ch != -1) {
				System.out.println(ch);
				ch = fileReader.read();
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void fileReaderTest3() {
		try {
			FileReader fileReader = new FileReader("file.txt");
			//public int read(char[] cbuf) 将字符读入数组
			//返回：读取的字符数，如果已到达流的末尾，则返回 -1 
			char[] buffer = new char[1024];
			int length = 0;
			while ((length = fileReader.read(buffer)) != -1) {
				System.out.println(length);
				System.out.println(Arrays.toString(buffer));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void fileCopy() {
		FileReader fileReader = null;
		FileWriter fileWriter = null;
		try {
			fileReader = new FileReader("file.txt");
			fileWriter = new FileWriter("file_back.txt");
			char[] buffer = new char[10];
			int length = 0;
			while ((length = fileReader.read(buffer)) != -1) {
				fileWriter.write(buffer);
			}
			//fileWriter.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileWriter != null) {
				try {
					fileWriter.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void fileCopy1() {
		FileReader fileReader = null;
		FileWriter fileWriter = null;
		try {
			fileReader = new FileReader("file.txt");
			fileWriter = new FileWriter("file_back.txt");
			char[] buffer = new char[10];
			int length = 0;
			while ((length = fileReader.read(buffer)) != -1) {
				//我读取了多少个就写多少个
				fileWriter.write(buffer, 0, length);
			}
			//fileWriter.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileWriter != null) {
				try {
					fileWriter.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void fileInputStreamCopy() {
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		try {
			fileInputStream = new FileInputStream("java.png");
			fileOutputStream = new FileOutputStream("java_back.png");
			byte[] buffer = new byte[1024];//字节数组
			int length = 0;
			while ((length = fileInputStream.read(buffer)) != -1) {
				fileOutputStream.write(buffer, 0, length);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}
